#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

/*================================================
 Program: tapregs.do
 Author:  Avi Ebenstein
 Created: August 2007
 Purpose: OLS regressions by tap water share
=================================================*/

global controls "yrsed farmer urban lnairpollution i.region i.cla3";

use ~/pollution/datafiles/dsp_basins.dta, replace;
global causelist "
deathrate9
deathrate_090
deathrate_091
deathrate_095
deathrate_099

";

global mylist "overall_q";

for any manufacturing2 paper coal electric: replace share_X=0 if share_X==.;
gen deathrate_099=deathrate9-deathrate_090-deathrate_091-deathrate_095;
gen maledr_099=maledr9-maledr_090-maledr_091-maledr_095;
gen femaledr_099=femaledr9-femaledr_090-femaledr_091-femaledr_095;

for any 090 091 095 099: gen lndeathrate_X=ln(deathrate_X);
for any 090 091 095 099: replace lndeathrate_X=0 if lndeathrate_X==.;

*capture drop urban;
gen urbanshare=share_tap_water1>.50;

forvalues k=0/1{; 
foreach j of global causelist{;
                 gen lndeathrate_`j'_`k'=ln(`j');
foreach i of global mylist{;
                           preserve;
                           replace `i'=. if `i'==0;
                           xi: reg lndeathrate_`j'_`k' `i' $controls [w=totalpop] if urbanshare==`k',cluster(provgb) robust;
                           gen beta=_b[`i'];
                           gen se=_se[`i'];
                           gen urbtype=`k';
                           capture drop avg;
                           egen avg=mean(`j');
                           gen t=beta/se;
                           gen str cause="`j'";
                           gen str chemical="`i'";
                           keep cause chemical beta se t avg urbtype;
                           keep in 1;
                           save ~/pollution/datafiles/run_`i'_`j'_`k', replace;
                           restore;
                         };
               };
              };

clear;
set obs 1;
                 gen a=1;
forvalues k=0/1{;                 
foreach j of global causelist{;
foreach i of global mylist{;
                           append using ~/pollution/datafiles/run_`i'_`j'_`k';
                         };
               };
};  
replace chemical="a_a_overall_q" if chemical=="overall_q";
replace cause="a_deathrate_080" if cause=="deathrate9";
replace cause="a_deathrate_090" if cause=="deathrate_090";
replace cause="a_deathrate_091" if cause=="deathrate_091";
replace cause="a_deathrate_095" if cause=="deathrate_095";
replace cause="a_deathrate_099" if cause=="deathrate_099";

capture log close;
gen dummy=1;
set linesize 250;
log using ~/pollution/logfiles/tapregs.log, replace;
table cause urbtype, c(mean beta mean se) f(%9.3fc) cellwidth(16);
capture log close;

!rm ~/pollution/datafiles/run_*.dta, force;                 
